package com.fany.fastsearcher.activity;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import android.widget.Switch;
import com.baidu.ops.appunion.sdk.AppUnionSDK;
import com.fany.fastsearcher.R;
import com.fany.utils.ConfigUtils;
import com.fany.views.circlemenu.CircleMenuItem;
import com.fany.views.circlemenu.CircleMenuView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
//import com.baidu.ops.appunion.sdk.AppUnionSDK;

public class MainActivity extends BaseActivity
{
    private CircleMenuView _menuView = null;
    private static final int SearchFilesCommandId = 0x101;
    private static final int BrowseFileCommandId = 0x102;
    private static final int AboutCommandId = 0x103;
    private static final int ToolboxCommandId = 0x104;

    public static final int File_List_Code = 0x100;

    private String _fileListPath = "/";
    public void searchFiles()
    {
        Intent intent = new Intent(this, FileSearchActivity.class);
        startActivity(intent);
    }

    public void browseFile()
    {
        Intent intent = new Intent(this, FileListActivity.class);
        intent.putExtra("path", _fileListPath);
        startActivityForResult(intent,File_List_Code);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (File_List_Code == requestCode)
        {
            Bundle bundle = null;
            if (data != null && (bundle = data.getExtras()) != null)
            {
               _fileListPath =bundle.getString("path");
            }
        }
    }

    public void about()
    {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    public void showToolbox()
    {
        AppUnionSDK.getInstance(MainActivity.this).showAppList();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        AppUnionSDK.getInstance(MainActivity.this).initSdk();
        ConfigUtils.getInstance().initConfiguration();

        _menuView = new CircleMenuView(MainActivity.this);
        _menuView.setBackgroundResource(R.drawable.menubkground);
        _menuView.addItem("浏览文件", R.drawable.browse_folder, BrowseFileCommandId);
        _menuView.addItem("文件搜索", R.drawable.search_file, SearchFilesCommandId);
        _menuView.addItem("工具箱", R.drawable.toolbox, ToolboxCommandId);
        _menuView.addItem("关于", R.drawable.help, AboutCommandId);
        setContentView(_menuView);
        new Thread(new CommandWatchThread()).start();
    }

    @Override
    protected void onDestroy()
    {
        AppUnionSDK.getInstance(MainActivity.this).quitSdk();
        super.onDestroy();
    }

    private Handler _handler = new CommandHandler(this);

    private static class CommandHandler extends Handler
    {
        private WeakReference<MainActivity> _activity;

        public CommandHandler(MainActivity activity)
        {
            _activity = new WeakReference<MainActivity>(activity);
        }

        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case SearchFilesCommandId:
                    _activity.get().searchFiles();
                    break;
                case BrowseFileCommandId:
                    _activity.get().browseFile();
                    break;
                case AboutCommandId:
                    _activity.get().about();
                    break;
                case ToolboxCommandId:
                    _activity.get().showToolbox();
                    break;
            }
            super.handleMessage(msg);
        }
    }

    class CommandWatchThread implements Runnable
    {
        public void run()
        {
            while (!Thread.currentThread().isInterrupted())
            {
                CircleMenuItem item = _menuView.getTouchedItem();
                if (item != null)
                {
                    Message message = new Message();
                    message.what = item.commandId;
                    _handler.sendMessage(message);
                }
                try
                {
                    _menuView.cleanTouchedItem();
                    Thread.sleep(10);
                }
                catch (InterruptedException e)
                {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
